package byggeTegner;

import geometry2D.PixelPoint;
import geometry2D.MetricPoint2D;
import geometry2D.UTM32Location;
import context.*;

import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.BorderFactory;


// ***************************************************************************
// *** CLASS: StatusLine
// ***************************************************************************
public class StatusLine extends JPanel implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final long serialVersionUID=0; // AD'HOC
  static final String TAG="mainFrame.StatusLine";

  private LoweredLabel description;
  private LoweredLabel scale;
  private LoweredLabel mode;
  private LoweredLabel locationX,locationY;
  private LoweredLabel pixelPosX,pixelPosY;
  private LoweredLabel meterPosX,meterPosY;
  
  // *************************************************************************
  // *** CONSTRUCTOR: StatusLine
  // *************************************************************************
  public StatusLine()
  { super();
    setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
    setAlignmentX(LEFT_ALIGNMENT);

    add(new JSeparator(SwingConstants.VERTICAL));
    add(description=new LoweredLabel());
    add(locationX=new LoweredLabel());  add(locationY=new LoweredLabel());
    add(pixelPosX=new LoweredLabel());  add(pixelPosY=new LoweredLabel());
    add(meterPosX=new LoweredLabel());  add(meterPosY=new LoweredLabel());
    add(scale=new LoweredLabel());
    add(mode=new LoweredLabel()); 

    setDescription("");
    setScale("1:500");
    setMode("");

  }

  // *************************************************************************
  // *** METHODS
  // *************************************************************************
  public void setDescription(String text) { description.setText(" "+text+' '); }
  public void setMode(String text) { mode.setText(" Mouse: "+text+' '); }
  public void setScale(String text) { scale.setText(" Scale: "+text+' '); }

  public void setMousePosition(DrawPanel drawPanel,PixelPoint pixelPoint)
  { pixelPosX.setText(" pixel X: "+((pixelPoint==null)?"unknown":pixelPoint.getX())+' ');
    pixelPosY.setText(" pixel Y: "+((pixelPoint==null)?"unknown":pixelPoint.getY())+' ');
    MetricPoint2D metricPoint=pixelPoint.toMetricPoint2D(drawPanel);
    meterPosX.setText(" meter X: "+((metricPoint==null)?"unknown":metricPoint.getX())+' ');
    meterPosY.setText(" meter Y: "+((metricPoint==null)?"unknown":metricPoint.getY())+' ');
    UTM32Location location=metricPoint.toUTM32Location(drawPanel);
    locationX.setText("UTM32: "+((location==null)?"unknown":location.getEast())+" EAST(X) ");
    locationY.setText("UTM32: "+((location==null)?"unknown":location.getNorth())+" NORTH(Y) ");
    CHECK_CONVERT(drawPanel,pixelPoint);
    CHECK_CONVERT(drawPanel,metricPoint);
  }
  
  private void CHECK_CONVERT(DrawPanel drawPanel,MetricPoint2D metricPoint)
  {
	UTM32Location location=metricPoint.toUTM32Location(drawPanel);
	MetricPoint2D checkPoint=location.toMetricPoint2D(drawPanel);
    double diffX=Math.abs(checkPoint.getX()-metricPoint.getX());
    double diffY=Math.abs(checkPoint.getY()-metricPoint.getY());
//	Log.d(TAG,"CHECK_CONVERT METRIC: diffX="+diffX+", diffY="+diffY);  
    if( (diffX<0.000001 ) & (diffY<0.000001) ) return;
	Log.d(TAG,"CHECK_CONVERT: metricX="+metricPoint.getX()+", checkX="+checkPoint.getX()
			              +", metricY="+metricPoint.getY()+", checkY="+checkPoint.getY());  
  }
  
  private void CHECK_CONVERT(DrawPanel drawPanel,PixelPoint pixelPoint)
  {
    MetricPoint2D metricPoint=pixelPoint.toMetricPoint2D(drawPanel);
    PixelPoint checkPoint=metricPoint.toPixelPoint(drawPanel);
    double diffX=Math.abs(checkPoint.getX()-pixelPoint.getX());
    double diffY=Math.abs(checkPoint.getY()-pixelPoint.getY());
//	Log.d(TAG,"CHECK_CONVERT PIXEL: diffX="+diffX+", diffY="+diffY);  
    if( (diffX<0.000001 ) & (diffY<0.000001) ) return;
	Log.d(TAG,"CHECK_CONVERT: pixelX="+pixelPoint.getX()+", checkX="+checkPoint.getX()
			              +", pixelY="+pixelPoint.getY()+", checkY="+checkPoint.getY());  
  }
}

// ***************************************************************************
// *** LOCAL CLASS: LoweredLabel
// ***************************************************************************
class LoweredLabel extends JLabel
{ static final long serialVersionUID=0; // AD'HOC

  public LoweredLabel()
  { setBorder(BorderFactory.createLoweredBevelBorder());
    setHorizontalAlignment(SwingConstants.CENTER);
  }
}